package com.wqm.step.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.wqm.step.dao.OneToManyDao;
import com.wqm.step.pojo.Customer;
import com.wqm.step.pojo.Order;

@Repository
public class OneToManyDaoImpl extends HibernateDaoSupport implements OneToManyDao {

	@Autowired
	public void setSuperSessionFactory(SessionFactory factory) {
		super.setSessionFactory(factory);
	}

	public void saveData(Customer customer, Order... o) {
		for (Order order : o) {// 建立关系
			customer.getOrders().add(order);
		}
		for (Order order : o) {// 维护外键
			order.setC(customer);
		}
		Session session = this.getSessionFactory().getCurrentSession();
		session.save(customer);
	}

}
